package com.ticket.web.controller.yanchu;

import com.ticket.common.annotation.Log;
import com.ticket.common.core.controller.BaseController;
import com.ticket.common.core.domain.AjaxResult;
import com.ticket.common.core.page.TableDataInfo;
import com.ticket.common.enums.BusinessType;
import com.ticket.common.utils.poi.ExcelUtil;
import com.ticket.system.domain.TYanchuClassify;
import com.ticket.system.service.ITYanchuClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类管理Controller
 * 
 * @author ruoyi
 * @date 2023-09-27
 */
@Api(description = "分类管理")
@RestController
@RequestMapping("/system/classify")
public class TYanchuClassifyController extends BaseController
{
    @Autowired
    private ITYanchuClassifyService tYanchuClassifyService;


    @ApiOperation("查询分类管理列表")
    @PreAuthorize("@ss.hasPermi('system:classify:list')")
    @GetMapping("/list")
    public TableDataInfo list(TYanchuClassify tYanchuClassify)
    {
        startPage();
        List<TYanchuClassify> list = tYanchuClassifyService.selectTYanchuClassifyList(tYanchuClassify);
        return getDataTable(list);
    }

    @ApiOperation("小程序查询分类管理列表")
    @GetMapping("/list/forAllUser")
    public TableDataInfo listForAllUser(TYanchuClassify tYanchuClassify)
    {
        startPage();
        List<TYanchuClassify> list = tYanchuClassifyService.selectTYanchuClassifyList(tYanchuClassify);
        return getDataTable(list);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:classify:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TYanchuClassify tYanchuClassify)
    {
        List<TYanchuClassify> list = tYanchuClassifyService.selectTYanchuClassifyList(tYanchuClassify);
        ExcelUtil<TYanchuClassify> util = new ExcelUtil<TYanchuClassify>(TYanchuClassify.class);
        util.exportExcel(response, list, "分类管理数据");
    }


    @ApiOperation("获取分类管理详细信息")
    @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Long",dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermi('system:classify:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tYanchuClassifyService.selectTYanchuClassifyById(id));
    }

    @ApiOperation("新增分类管理")
    @ApiImplicitParam(name = "tYanchuClassify",value = "分类管理实体",dataType = "TYanchuClassify",dataTypeClass = TYanchuClassify.class)
    @PreAuthorize("@ss.hasPermi('system:classify:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TYanchuClassify tYanchuClassify)
    {
        Assert.notNull(tYanchuClassify.getClassifyName(),"分类名称不能为空");
        Assert.notNull(tYanchuClassify.getClassifyUrl(),"分类封面地址不能为空");
        //校验名称是否已存在
        TYanchuClassify classify = new TYanchuClassify();
        classify.setClassifyName(tYanchuClassify.getClassifyName());
        if (CollectionUtils.isNotEmpty(tYanchuClassifyService.selectTYanchuClassifyList(classify))){
            return AjaxResult.error("当前分类名称已存在,请重新设置");
        }
        tYanchuClassify.setClassifyNum(0);
        return toAjax(tYanchuClassifyService.insertTYanchuClassify(tYanchuClassify));
    }

    @ApiOperation("修改分类管理")
    @ApiImplicitParam(name = "tYanchuClassify",value = "分类管理实体",dataType = "TYanchuClassify",dataTypeClass = TYanchuClassify.class)
    @PreAuthorize("@ss.hasPermi('system:classify:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TYanchuClassify tYanchuClassify)
    {
        Assert.notNull(tYanchuClassify.getId(),"关键字段缺失");
        //校验名称是否已存在
        TYanchuClassify classifyOne = tYanchuClassifyService.selectTYanchuClassifyById(tYanchuClassify.getId());
        if (!tYanchuClassify.getClassifyName().equals(classifyOne.getClassifyName())){
            TYanchuClassify classify = new TYanchuClassify();
            classify.setClassifyName(tYanchuClassify.getClassifyName());
            if (CollectionUtils.isNotEmpty(tYanchuClassifyService.selectTYanchuClassifyList(classify))){
                return AjaxResult.error("当前分类名称已存在,请重新设置");
            }
        }
        return toAjax(tYanchuClassifyService.updateTYanchuClassify(tYanchuClassify));
    }

    @ApiOperation("删除分类管理")
    @ApiImplicitParam(name = "ids",value = "主键id，多个逗号拼接",required = true,dataType = "String",dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('system:classify:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids)
    {
        String[] split = ids.split(",");
        List<Long> collect = Arrays.stream(split).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
        return toAjax(tYanchuClassifyService.deleteTYanchuClassifyByIds(collect));
    }

    @ApiOperation("上移下移")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键id",required = true,dataTypeClass = Long.class,dataType = "Long"),
            @ApiImplicitParam(name = "type",value = "1:上移;2:下移",required = true,dataTypeClass = Integer.class,dataType = "Integer"),
            @ApiImplicitParam(name = "orderNum",value = "排序值",required = true,dataTypeClass = Integer.class,dataType = "Integer"),
    })
    @PostMapping("/upOrDown")
    public AjaxResult upOrDown(@RequestParam Long id, @RequestParam Integer type, @RequestParam Integer orderNum){
        tYanchuClassifyService.upOrDown(id,type,orderNum);
        return AjaxResult.success();
    }
}
